# sound_creator.py
import numpy as np
from scipy.io import wavfile
import sounddevice as sd


def sound_creator(frequency, duration, amplitude=1.0, sample_rate=44100):
    """
    生成指定频率的正弦波声音
    """
    t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
    wave = amplitude * np.sin(2 * np.pi * frequency * t)
    return wave


def save_sound_into_file(file_name, frequency, duration):
    """
    将生成的声音保存为WAV文件
    """
    wave = sound_creator(frequency, duration)
    wavfile.write(file_name, 44100, (wave * 32767).astype(np.int16))


def play_sound(frequency, duration):
    """
    播放生成的声音
    """
    wave = sound_creator(frequency, duration)
    sd.play(wave, 44100, blocking=True)


if __name__ == "__main__":
    path = "sounds/sound.wav"
    target_frequency = [18500, 18700, 18900,19100]
    save_sound_into_file(path, target_frequency[2], 2)
